﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Accouting804.Setting;
using Xamarin.Forms;

namespace Accouting804.Services
{

    /// <summary>
    /// 记账记录的数据存储类
    /// </summary>
    public class AccountItemDataStore : IDataStoreWithFilterQuery<AccountRecord>
    {
        WebClient client = new WebClient();
        IEnumerable<AccountRecord> items = new List<AccountRecord>();
        //bool IsConnected => Connectivity.NetworkAccess == NetworkAccess.Internet;
        public AccountItemDataStore()
        {
        }

        public async Task<bool> AddItemAsync(AccountRecord item)
        {
            var db = InitializeSetting.AccountAsyncConnection();
            try
            {
                await db.InsertAsync(item);
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public async Task<bool> DeleteItemAsync(string id)
        {

            if (id != null)
            {
                try
                {
                    var idInt = int.Parse(id);

                    var table = InitializeSetting.AccountAsyncConnection().Table<AccountRecord>();
                    await table.DeleteAsync(a => a.AutoId == idInt);


                    return true;
                }
                catch (Exception ex)
                {


                    return false;
                    throw;
                }

            }
            else
            {
                throw new Exception("id null");
            }
        }

        public async Task<AccountRecord> GetItemAsync(string id)
        {
            if (id != null)
            {
                var db = InitializeSetting.AccountAsyncConnection();
                var table = db.Table<AccountRecord>();
                try
                {
                    var founded = await db.FindAsync<AccountRecord>(id);
                    return founded;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return null;
        }

        public async Task<IEnumerable<AccountRecord>> GetItemsAsync(bool forceRefresh = false)
        {
            if (forceRefresh)
            {
                var db = InitializeSetting.AccountAsyncConnection();
                var table = db.Table<AccountRecord>();
                try
                {
                    var list = await table.ToListAsync();
                    return list;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return items;
        }

        public async Task<IEnumerable<AccountRecord>> GetItemsFilterQueryAsync(bool forceRefresh = false, string[] queryfilters = null)
        {
            if (forceRefresh)
            {
                SQLite.SQLiteAsyncConnection db = InitializeSetting.AccountAsyncConnection();
                var table = db.Table<AccountRecord>();
                var list = await table.ToListAsync();
                return list;
            }
            return items;
            throw new NotImplementedException();
        }

        public async Task<bool> UpdateItemAsync(AccountRecord item)
        {
            var db = InitializeSetting.AccountAsyncConnection();
            var scalar = db.UpdateAsync(item);

            var changedRow = await scalar;

            if (changedRow >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }


            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 账本的数据存储类
    /// </summary>
    public class BookDataStore : IDataStoreWithFilterQuery<Book>, IDataStoreWithWhereQuery<Book>
    {

        List<Book> books;

        public BookDataStore()
        {
            books = InitializeSetting.AccountConnection().Table<Book>().ToList();
        }
        public Task<bool> AddItemAsync(Book item)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DeleteItemAsync(string id)
        {
            throw new NotImplementedException();
        }

        public Task<Book> GetItemAsync(string id)
        {
            throw new NotImplementedException();
        }

        public async Task<IEnumerable<Book>> GetItemsAsync(bool forceRefresh = false)
        {
            return await Task.FromResult(books);
        }

        public Task<IEnumerable<Book>> GetItemsFilterQueryAsync(bool forceRefresh = false, string[] queryfilters = null)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Book> GetItemsWithWhere(Func<Book, bool> func, bool forceRefresh = false) => books.Where(func);

        public Task<IEnumerable<Book>> GetItemsWithWhereTask(Expression<Func<Book, bool>> expression, bool forceRefresh = false)
        {
            throw new NotImplementedException();
        }

        public Task<bool> UpdateItemAsync(Book item)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 分类的数据存储类
    /// </summary>
    public class CategoryDataStore : IDataStoreWithFilterQuery<Category>
    {
        List<Category> categories;

        public CategoryDataStore()
        {
            using (var db = InitializeSetting.AccountConnection())
            {
                categories = db.Table<Category>().ToList();
            }


        }

        public Task<bool> AddItemAsync(Category item)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DeleteItemAsync(string id)
        {
            throw new NotImplementedException();
        }

        public Task<Category> GetItemAsync(string id)
        {
            throw new NotImplementedException();
        }

        public async Task<IEnumerable<Category>> GetItemsAsync(bool forceRefresh = false)
        {
            return await Task.FromResult(categories);

        }

        public Task<IEnumerable<Category>> GetItemsFilterQueryAsync(bool forceRefresh = false, string[] queryfilters = null)
        {
            throw new NotImplementedException();
        }

        public Task<bool> UpdateItemAsync(Category item)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 支付方式数据存储
    /// </summary>
    public class PaymentMethodDataStore : IDataStoreWithFilterQuery<PaymentMethod>, IDataStoreWithWhereQuery<PaymentMethod>
    {
        List<PaymentMethod> paymentMethods;

        public PaymentMethodDataStore()
        {
            //InitializeSetting
            //    .AccountAsyncConnection()
            //    .Table<PaymentMethod>()
            //    .ToListAsync()
            //    .ContinueWith(task => paymentMethods = task.Result);
            paymentMethods = InitializeSetting.AccountConnection().Table<PaymentMethod>().ToList();
        }
        public Task<bool> AddItemAsync(PaymentMethod item)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DeleteItemAsync(string id)
        {
            throw new NotImplementedException();
        }

        public Task<PaymentMethod> GetItemAsync(string id)
        {
            throw new NotImplementedException();
        }

        public async Task<IEnumerable<PaymentMethod>> GetItemsAsync(bool forceRefresh = false)
        {
            return await Task.FromResult(this.paymentMethods);
        }

        public Task<IEnumerable<PaymentMethod>> GetItemsFilterQueryAsync(bool forceRefresh = false, string[] queryfilters = null)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<PaymentMethod> GetItemsWithWhere(Func<PaymentMethod, bool> func, bool forceRefresh = false)
        {
            return paymentMethods.Where(func);
        }

        public async Task<IEnumerable<PaymentMethod>> GetItemsWithWhereTask(Expression<Func<PaymentMethod, bool>> expression, bool forceRefresh = false)
        {
            return await Task.FromResult(this.paymentMethods.Where(expression.Compile()));
        }

        public Task<bool> UpdateItemAsync(PaymentMethod item)
        {
            throw new NotImplementedException();
        }
    }

}